#ifndef MGL_LINUX_H_INCLUDED
#define MGL_LINUX_H_INCLUDED
#include"jawt_md.h"
#include "nativepart.h"
#include "linuxcontextinfo.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void throwexception(JNIEnv *env,char* exc);

JNIEXPORT void JNICALL Java_multilwjgl_multiglcanvas_initgl
  (JNIEnv *env, jobject canvas, jint bpp, jint alpha, jint depth, jint stencil)
  {


      cout<<"called";
  jclass jc = env->GetObjectClass(canvas);
jfieldID id = env->GetFieldID(jc, "contextptr", "J");

jlong clong =env->GetLongField(canvas,id);

contextinfo* cinfo;

if(clong==0)
{
 cinfo=new contextinfo();
 env->SetLongField(canvas,id,(long)cinfo);

  JAWT_DrawingSurface *ds;
  JAWT_DrawingSurfaceInfo *dsi;
  JAWT_X11DrawingSurfaceInfo *dsi_win;

  jboolean result;

  cinfo->awt.version = JAWT_VERSION_1_4;
  result = JAWT_GetAWT(env, &cinfo->awt);
cout<<"got awt";
if(result==JNI_FALSE)
{
 throwexception(env,"could not get awt");
 return;
}

ds = cinfo->awt.GetDrawingSurface(env, canvas);

cinfo->awt.Lock(env);


cout<<"lock";

 dsi = ds->GetDrawingSurfaceInfo(ds);
  dsi_win = (JAWT_X11DrawingSurfaceInfo *) dsi->platformInfo;

 cinfo-> display = dsi_win->display;
 cinfo-> window = dsi_win->drawable;

 if (!cinfo->display) {
     throwexception(env,"no display");
     return;
 }

 if (!cinfo->window) {
     throwexception(env,"no drawable");
     return;
 }


 vector<int> attributes;

attributes.push_back(GLX_RGBA);
attributes.push_back(GLX_DOUBLEBUFFER);
attributes.push_back(GLX_RGBA);

int bpe=convertToBPE(bpp);

attributes.push_back(GLX_RED_SIZE);
attributes.push_back(bpe);
attributes.push_back(GLX_GREEN_SIZE);
attributes.push_back(bpe);
attributes.push_back(GLX_BLUE_SIZE);
attributes.push_back(bpe);

attributes.push_back(GLX_ALPHA_SIZE);
attributes.push_back(alpha);

attributes.push_back(GLX_DEPTH_SIZE);
attributes.push_back(depth);

attributes.push_back(GLX_STENCIL_SIZE);
attributes.push_back(stencil);
attributes.push_back(None);



XVisualInfo *vi = glXChooseVisual(cinfo->display, DefaultScreen(cinfo->display), &attributes[0]);

 if (vi == NULL) {
     throwexception(env,"couldn't get visual");

 }


cinfo->cx = glXCreateContext(cinfo->display, vi, None, true);

 if (cinfo->cx == NULL) {
    throwexception(env,"couldn't create context");




 }

 glXMakeCurrent(cinfo->display,cinfo-> window, cinfo->cx);

cinfo->awt.Unlock(env);


}
else{

throwexception(env, "canvas has already been initialized\nmust destroy before reinitializing");
return;
}


  };

void throwexception(JNIEnv *env,char* exc)
{

 jclass excs = env->FindClass("java/lang/Exception");
         env->ThrowNew(excs, exc);
}


JNIEXPORT void JNICALL Java_multilwjgl_multiglcanvas_currentize
  (JNIEnv *env, jobject canvas, jboolean currentize)
  {

       jclass jc = env->GetObjectClass(canvas);
jfieldID id = env->GetFieldID(jc, "contextptr", "J");

jlong clong =env->GetLongField(canvas,id);

contextinfo* cinfo=(contextinfo*)clong;

JAWT_DrawingSurface* ds = cinfo->awt.GetDrawingSurface(env, canvas);


cinfo->awt.Lock(env);


if(currentize)
{
    glXMakeCurrent(cinfo->display,cinfo-> window, cinfo->cx);
}
else{
 glXMakeCurrent(cinfo->display,None, NULL);
}

cinfo->awt.Unlock(env);

  }


JNIEXPORT void JNICALL Java_multilwjgl_multiglcanvas_test
  (JNIEnv *env, jobject canvas)
  {
        jclass jc = env->GetObjectClass(canvas);
jfieldID id = env->GetFieldID(jc, "contextptr", "J");

jlong clong =env->GetLongField(canvas,id);

contextinfo* cinfo=(contextinfo*)clong;


  JAWT_DrawingSurface *ds;
  ds = cinfo->awt.GetDrawingSurface(env, canvas);
 cinfo->awt.Lock(env);

  glXMakeCurrent(cinfo->display, cinfo->window, cinfo->cx);

  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  glBegin( GL_TRIANGLES );
  glColor3f(1.0f, 0.0f, 0.0f);
  glVertex2f(0.0f, 1.0f);
  glColor3f(0.0f, 1.0f, 0.0f);
  glVertex2f(0.87f, -0.5f);
  glColor3f(0.0f, 0.0f, 1.0f);
  glVertex2f(-0.87f, -0.5f);
  glEnd();
  glPopMatrix();

  glFlush();

  glXSwapBuffers(cinfo->display, cinfo->window);

cinfo-> awt.Unlock(env);
  }


JNIEXPORT void JNICALL Java_multilwjgl_multiglcanvas_swap
  (JNIEnv *env, jobject canvas)
  {



             jclass jc = env->GetObjectClass(canvas);
jfieldID id = env->GetFieldID(jc, "contextptr", "J");

jlong clong =env->GetLongField(canvas,id);

contextinfo* cinfo=(contextinfo*)clong;

JAWT_DrawingSurface* ds = cinfo->awt.GetDrawingSurface(env, canvas);

  cinfo->awt.Lock(env);



 glXSwapBuffers(cinfo->display, cinfo->window);

cinfo->awt.Unlock(env);

  }
#endif // MGL_LINUX_H_INCLUDED
